// miniprogram/pages/adminclass/classdetail.js
import Dialog from '/@vant/weapp/dialog/dialog';
import Toast from '/@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    student_info1: [],
    student_info2: [],
    number:'',
    all: '0',
    tab: '时间降序',
    toast: false,
    thispage: 1, //记录当前页面试哪个
    page1: 0,
    page2: 0,
  },

  onLoad: function(options) {
    if (this.data.number.length == 0) {
      this.data.number = options.number
    }
    const db = wx.cloud.database()
    db.collection('card').orderBy('time', 'desc').where({
      number: this.data.number
    }).get({
      success: res => {
        console.log(res)
        this.setData({
          student_info1: res.data
        })
        this.count()
        // console.log(this.data.student_info)
      },
      fail: err => {
        wx.showToast({
          icon: "none",
          title: '查询记录失败',
        })
      }
    })
    db.collection('card').where({
      number: this.data.number
    }).count({
      success: res => {
        this.setData({
          all: res.total
        })
      }
    })
    db.collection('card').orderBy('student.number', 'asc').where({
      number: this.data.number
    }).get({
      success: res => {
        console.log(res)
        this.setData({
          student_info2: res.data
        })
        // console.log(this.data.student_info)
      },
      fail: err => {
        wx.showToast({
          icon: "none",
          title: '查询记录失败',
        })
      }
    })
  },
  onShow:function(){
    this.onLoad()
  },
  load1: function() { //对时间排序的分页加载
    console.log('jiazai111')
    this.setData({
      load: true
    })
    let that = this
    let shu = 20
    let dir = this.data.student_info1
    that.data.page1 = that.data.page1 + 20
    const db = wx.cloud.database()
    db.collection('card').orderBy('time', 'desc').skip(this.data.page1).limit(shu).where({
      number: this.data.number
    }).get({
      success: res => {
        dir = dir.concat(res.data)
        this.data.student_info1 = dir
        this.setData({
          student_info1: this.data.student_info1,
          load: false
        })
        // console.log(this.data.student_info)
      },
      fail: err => {
        wx.showToast({
          icon: "none",
          title: '人家也是有底线的哦！',
        })
      }
    })
  },
  load2: function() { // 对学号升序的分页加载
    console.log('jiazai2222')
    this.setData({
      load: true
    })
    let that = this
    let shu = 20
    let dir = this.data.student_info2
    that.data.page2 = that.data.page2 + 20
    const db = wx.cloud.database()
    db.collection('card').orderBy('student.number', 'asc').skip(this.data.page2).limit(shu).where({
      number: this.data.number
    }).get({
      success: res => {
        dir = dir.concat(res.data)
        this.data.student_info2 = dir
        this.setData({
          student_info2: this.data.student_info2,
          load: false
        })
        // console.log(this.data.student_info)
      },
      fail: err => {
        wx.showToast({
          icon: "none",
          title: '人家也是有底线的哦！',
        })
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    this.onLoad()
    wx.hideNavigationBarLoading();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    console.log(this.data.page2)
    if (this.data.thispage == 1) {
      if (this.data.page1 <= this.data.all) {
        wx.hideNavigationBarLoading(); //隐藏加载
        wx.stopPullDownRefresh();
        this.load1()
      }
    } else if (this.data.thispage == 2) {
      if (this.data.page2 <= this.data.all) {
        wx.hideNavigationBarLoading(); //隐藏加载
        wx.stopPullDownRefresh();
        this.load2()
      }
    }
  },
  onChangeTab(event) { // 头部标签
    wx.showToast({
      title: `切换到 ${event.detail.name}`,
      icon: 'none'
    });
    if (event.detail.name == '时间排序') {
      this.setData({
        thispage: 1
      })
    } else {
      this.setData({
        thispage: 2
      })
    }
  },
  tap: function(e) {
    // console.log(e)  
    wx.navigateTo({
      url: './historyCard?_id=' + e.target.dataset.id,
    })
  }
})